#include "dbc.h"
#include <time.h>

DBConnect::DBConnect(QString dbms, QString db_name, QString db_host, QString db_username, QString db_password)
{
  char buffer[11];
  sprintf(buffer, "%d", (int)time(NULL));
  this->db = QSqlDatabase::addDatabase(dbms, dbms + buffer);
  this->db.setHostName(db_host);
  this->db.setDatabaseName(db_name);
  this->db.setUserName(db_username);
  this->db.setPassword(db_password);

  this->dbms = dbms;
  this->status = db.open();

  this->query = new QSqlQuery(this->db);
}

DBConnect::~DBConnect(){
  delete this->query;
  closeConnection();
}

void DBConnect::execQuery(QString str) 
{
  bool status = query->exec(str);
  if(status == false){
    qDebug() << "Error while executing query!" << db.lastError();
  }
}

void DBConnect::closeConnection()
{
  if(this->db.isOpen())
  {
    this->db.close();
  }
}

bool DBConnect::isOpen()
{
  return this->db.isOpen();
}
